package com.example.museum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.museum.common.Result;
import com.example.museum.entity.Product;
import com.example.museum.entity.ProductAppraise;
import com.example.museum.service.ProductService;
import com.example.museum.vo.ProductAppraiseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 */
@RestController
@RequestMapping("product")
@Slf4j
public class ProductController {


    @Autowired
    private ProductService productService;

    /**
     *  分页查询
     * @param page
     * @param pageSize
     * @param query
     * @return
     */
    @GetMapping("pagelist")
    public Result<Page> page(int page, int pageSize, String query){
        log.info("page = {},pageSize = {},name = {}", page, pageSize, query);
       Page<Product>  productList = productService.getpageList(page,pageSize,query);
        return Result.success(productList);
    }

    /**
     *  查询所有商品
     * @return
     */
    @GetMapping("list")
    public Result<List<Product>> list() {
        List<Product> products = productService.getlist();
        return Result.success(products);
    }

    /**
     * 写评论
     * @param productAppraise
     * @return
     */
    @PostMapping("apprasie")
    public Result addendAppraise(@RequestBody ProductAppraise productAppraise) {
        log.info("商品为:{}",productAppraise);
        productService.addendAppraise(productAppraise);
        return Result.success("成功");

    }

    /**
     * 获取评论
     * @param productId
     * @return
     */
    @GetMapping("apprasielist/{productId}")
    public Result<List<ProductAppraiseVo>> getapprasielist(@PathVariable Long productId) {
        log.info("商品为id为:{}",productId);
       List<ProductAppraiseVo> productAppraiselist=productService.getapprasielist(productId);
        return Result.success(productAppraiselist);
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody Product product) {
        log.info("商品数据为:{}",product);
        productService.addproduct(product);
        return Result.success("成功");
    }

    /**
     * 获取商品细节
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public Result getbyid(@PathVariable Long id){
        log.info("商品id为:{}",id);
        Product product = productService.getById(id);
        return Result.success(product);
    }

    /**
     * 更新商品
     * @param product
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Product product) {
        log.info("商品信息为:{}",product);
        productService.updateProduct(product);
        return Result.success("成功");
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        log.info("删除的商品id为:{}",id);
        productService.deleteByid(id);
        return Result.success("成功");
    }



    /**
     * 分页查询商品评价
     * @param page 页码
     * @param pageSize 每页大小
     * @param query 查询条件（商品名称）
     * @param rating 评分筛选
     * @return 评价分页结果
     */
    @GetMapping("appraises")
    public Result<Page<ProductAppraiseVo>> getAppraisePageList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String query,
            @RequestParam(required = false) Integer rating) {
        
        log.info("分页查询商品评价: page={}, pageSize={}, query={}, rating={}", page, pageSize, query, rating);
        Page<ProductAppraiseVo> appraisePage = productService.getAppraisePageList(page, pageSize, query, rating);
        return Result.success(appraisePage);
    }

    /**
     * 删除商品评价
     * @param id 评价ID
     * @return 操作结果
     */
    @DeleteMapping("appraise/{id}")
    public Result deleteAppraise(@PathVariable Long id) {
        log.info("删除商品评价: id={}", id);
        productService.deleteAppraise(id);
        return Result.success("删除成功");
    }
}
